import java.util.ArrayList;

import junit.framework.*;

public class CasoDeTeste extends junit.framework.TestCase {

	private State estado;
	private Track t1,t2,t3,t4;
	private Train tr1;
	private PointIn p1;
	private PointOut p2;
	private Semaphore sema1;
	private Semaphore sema2;
	private Semaphore sema3;
	
	protected void setUp(){
		estado = new State();
		ArrayList over = new ArrayList();
		over.add((Seg) t4);
		t1 = new Track();
		t2 = new Track();
		t3 = new Track();
		t4 = new Track();
		tr1 = new Train();
		sema1 = new Semaphore();
		sema2 = new Semaphore();
		sema3 = new Semaphore();
		p2 = new PointOut(sema2);
		p1 = new PointIn(p2);
	}

	public void testMove(){
		try {
			estado.trains.put(tr1, t1);
			estado.move(tr1, t1);
		} catch (CantMoveException e) {
			System.out.println("Cant Move");
		}
		assertEquals(0, estado.trains.keySet().size());
	}
	
	public void testToGreen(){
		try {
			t1.setSema(sema1);
			estado.reds.add(sema1);
			estado.toGreen((Seg) t1);
		} catch (Exception e) {
			System.out.println("Cant Set");
		}
		assertEquals(0, estado.reds.size());
	}
	
	public void testToRed(){
		try {
			t1.setSema(sema1);
			estado.toRed(t1);
		} catch (CantSetRedException e) {
			System.out.println("Cant Set");
		}
		assertEquals(1, estado.reds.size());
	}
	
	public void testToOn(){
		try {
			estado.toOn(p1, p2);
		} catch (CantSetOnException e) {
			System.out.println("Cant Set");
		}
		assertEquals(2, estado.ons.size());
	}
	
	public void testToOff(){
		try {
			estado.toOn(p1, p2);
			estado.toRed(p2);
			estado.toOff(p1, p2);
		} catch (Exception e) {
			System.out.println("Cant Set");
		}
		assertEquals(0, estado.ons.size());
	}
	
	public void testComplex1(){
		t2.setNext((Seg) t1);
		t3.setNext((Seg) t2);
		ArrayList over = new ArrayList();
		over.add((Seg) t4);
		t3.overlaps = over;
		t4.sema = sema1;
		t3.sema = sema2;
		estado.reds.add(sema2);
		try {
			estado.move(tr1, (Seg) t4);
		} catch (CantMoveException e) {
			System.out.println("Cant Move");
		}
		assertEquals(1, estado.reds.size());
		assertEquals(1, estado.trains.size());
	}
	
	public void testComplex2(){
		t2.setNext((Seg) t1);
		t3.setNext((Seg) t2);
		ArrayList over = new ArrayList();
		over.add((Seg) t4);
		t3.overlaps = over;
		t4.sema = sema1;
		t3.sema = sema2;
		estado.reds.add(sema1);
		try {
			estado.move(tr1, (Seg) t3);
		} catch (CantMoveException e) {
			System.out.println("Cant Move");
		}
		assertEquals(1, estado.reds.size());
		assertEquals(1, estado.trains.size());
	}
	
	public void testExceptionGreen(){
		t1.setSema(sema1);
		try {
			estado.toGreen((Seg) t1);
		} catch (Exception e) {
			System.out.println("Cant Set");
		}
		assertEquals(0, estado.reds.size());
	}
	
	public static Test suite(){
		TestSuite suite= new TestSuite(State.class);
		return suite;
	}
	
}
